﻿using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Rodunu.Business.Logic;
using Rodunu.Business.Models.Movies;
using Rodunu.Business.Interfaces;
using Rodunu.Clients.WPF.Controls;

namespace Rodunu.Clients.WPF.Collection
{
    /// <summary>
    /// Interaction logic for CollectionMain.xaml
    /// </summary>
    public partial class CollectionMain : UserControl
    {
        private readonly IList<Title> _myTitles;
        private int _nextId;
        private readonly ICollection _collectionBL;

        public CollectionMain(IManageSettings manageSettings)
        {
            InitializeComponent();

            SetUpEvents();

            _collectionBL = new CollectionBL(manageSettings);
            _myTitles = _collectionBL.GetMyTitles();

            //TODO: load users movies from online if local store not found
            if (_myTitles != null && _myTitles.Count > 0)
            {
                _nextId = _myTitles.OrderBy(o => o.Id).Last().Id + 1;
            }
            else
            {
                if (_myTitles == null)
                    _myTitles = new List<Title>();
            }

            BrowseButton_Click(null, new RoutedEventArgs());
        }

        private void SetUpEvents()
        {
            AddButton.Click += AddButton_Click;
            BrowseButton.Click += BrowseButton_Click;
            SaveNewButton.Click += SaveNewButton_Click;
        }

        private void SetControlVisible(UIElement element)
        {
            collectionHolder.Children.Clear();
            collectionHolder.Children.Add(element);
        }

        //private void HideAll()
        //{
        //    AddMovieToCollection.Visibility = System.Windows.Visibility.Collapsed;
        //    DetailPanel.Visibility = System.Windows.Visibility.Collapsed;
        //    CollectionPanel.Visibility = System.Windows.Visibility.Collapsed;
        //    MyCollection.Visibility = System.Windows.Visibility.Collapsed;
        //}

        void BrowseButton_Click(object sender, RoutedEventArgs e)
        {
            //HideAll();
            //CollectionPanel.Visibility = System.Windows.Visibility.Visible;
            //MyCollection.Visibility = System.Windows.Visibility.Visible;
            Browse2d browse2d = new Browse2d();
            SetControlVisible(browse2d);
            browse2d.CreateObjects(_myTitles);
            

            //TODO: default this to icon view when done
            //MyCollection.ShowList(_myTitles);
        }

        void SaveNewButton_Click(object sender, RoutedEventArgs e)
        {
            Cursor = Cursors.Wait;

            AddMovieToCollection addMovie = new AddMovieToCollection();

            Title newTitle = addMovie.GetNewMovie();
            newTitle.Id = _nextId;
            _nextId++;
            IList<Title> titles = new List<Title>();
            foreach (var title in _myTitles)
            {
                titles.Add(title);
            }
            titles.Add(newTitle);
            _collectionBL.Save(titles);
            //AddMovieToCollection.Visibility = System.Windows.Visibility.Collapsed;
            DetailPanel.Visibility = System.Windows.Visibility.Collapsed;

            BrowseButton_Click(sender, e);

            Cursor = Cursors.Arrow;
        }

        void AddButton_Click(object sender, RoutedEventArgs e)
        {
            //HideAll();
            //AddMovieToCollection.Visibility = System.Windows.Visibility.Visible;
            AddMovieToCollection addMovieCollection = new AddMovieToCollection();
            SetControlVisible(addMovieCollection);
            DetailPanel.Visibility = System.Windows.Visibility.Visible;
        }
    }
}
